package com.michael.demos.base.java.sort;

import java.util.Map;
import java.util.Random;
import java.util.TreeMap;

/**
 * 类功能描述:
 * <pre>
 *   排序测试类
 * </pre>
 *
 * @author Michael
 * @version 1.0
 * @date 2020/9/24 10:34
 */
public class SortClient {

    public static final Random R = new Random();

    public static Map<Long, String> COST_TIME_MAP = new TreeMap<>(Long::compareTo);

    public static void main(String[] args) {

        //doTestSort(100,10);
        //doTestSort(100,100);
        //doTestSort(100,1000);
        //doTestSort(100,10000);
        //doTestSort(100,100000);

        //int[] sourceArray = initSource(30, 100, 200);
        //printArray(sourceArray);
        int[] sourceArray = new int[]{91, -23, -16, 48, 37, 56, -92, -74, 0, 54, 28};
        int[] sorted = new HeapSort().sort(sourceArray);
        printArray(sorted);
    }

    public static void doTestSort(int testCount, int arrLength) {
        for (int i = 0; i < testCount; i++) {

            int[] sourceArray = initSource(arrLength);
            //testSort(new BubbleSort(), sourceArray);
            //testSort(new BucketSort(), sourceArray);
            //testSort(new CountingSort(), sourceArray);
            //testSort(new HeapSort(), sourceArray);
            //testSort(new InsertSort(), sourceArray);
            //testSort(new MergeSort(), sourceArray);
            //testSort(new QuickSort(), sourceArray);
            testSort(new RadixSort(), sourceArray);
            //testSort(new SelectionSort(), sourceArray);
            //testSort(new ShellSort(), sourceArray);
            COST_TIME_MAP.forEach((k, v) -> System.out.println(arrLength + "\t" + k + "\t" + v));
            COST_TIME_MAP = new TreeMap<>();
        }
    }

    public static void testSort(IArraySort sort, int[] sourceArray) {
        long beforeNano = System.nanoTime();
        //int[] afterArr = sort.sort(sourceArray);
        sort.sort(sourceArray);
        long afterNano = System.nanoTime();
        //System.out.println("排序后：");
        //printArray(afterArr);

        String sortName = sort.getClass().getSimpleName();
        Long sortCost = afterNano - beforeNano;

        COST_TIME_MAP.put(sortCost, sortName);
    }

    public static int[] initSource(int count, int min, int max) {

        int[] resultArray = new int[count];
        for (int i = 0; i < count; i++) {
            resultArray[i] = R.nextInt(max - min) + min;
        }
        return resultArray;
    }

    public static int[] initSource(int count) {

        int[] resultArray = new int[count];
        for (int i = 0; i < count; i++) {
            resultArray[i] = R.nextInt(count);
        }
        return resultArray;
    }

    public static void printArray(int[] sourceArray) {
        int length = sourceArray.length;
        System.out.print("[");
        for (int i = 0; i < length; i++) {
            System.out.print(sourceArray[i]);
            if (i == length - 1) {
                System.out.println("]");
            } else {
                System.out.print(",");
            }
        }
    }
}
